package com.ruoyi.vote.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.vote.admin.domain.bo.VoteCertificateReceiveBo;
import com.ruoyi.vote.admin.domain.bo.VoteFeedbackBo;
import com.ruoyi.vote.admin.domain.bo.VoteInteractBo;
import com.ruoyi.vote.admin.domain.bo.VotePrizeReceiveBo;
import com.ruoyi.vote.admin.domain.vo.VoteActivityVo;
import com.ruoyi.vote.admin.domain.vo.VoteCertificateReceiveVo;
import com.ruoyi.vote.admin.domain.vo.VotePrizeReceiveVo;
import com.ruoyi.vote.admin.service.*;
import com.ruoyi.vote.app.domain.bo.ApiPrizeReceiveBo;
import com.ruoyi.vote.app.domain.request.VoteActivityReq;
import com.ruoyi.weixin.domain.WxUser;
import com.ruoyi.weixin.service.WxUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API - 我的相关
 * @author 立勤
 * @date 2023/07/18 11:08:10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/member")
@Slf4j
public class ApiMemberController extends BaseController {

    private final WxUserService wxUserService;
    private final IVoteActivityService activityService;
    private final IVotePrizeReceiveService prizeReceiveService;
    private final IVoteCertificateReceiveService certificateReceiveService;
    private final IVoteFeedbackService feedbackService;
    private final IVoteInteractService iVoteInteractService;

    /**
     * 我的参加活动
     * @param req       查询条件
     * @param pageQuery 分页
     * @return
     */
    @GetMapping("/participated")
    public TableDataInfo<VoteActivityVo> iParticipatedIn(VoteActivityReq req, PageQuery pageQuery){
        if (ObjectUtil.isNull(req.getVoteStatus())){
            throw new RuntimeException("请设置活动状态");
        }
        req.setMId(LoginHelper.getUserId());
        return activityService.queryPageList(req,pageQuery);
    }

    /**
     * 我的奖品
     * @return
     */
    @GetMapping("/my-prize")
    public TableDataInfo<VotePrizeReceiveVo> myPrize(PageQuery pageQuery){
        return prizeReceiveService.queryPageList(new VotePrizeReceiveBo() ,pageQuery);
    }

    /**
     * 我的证书
     * @return
     */
    @GetMapping("/my-certificate")
    public TableDataInfo<VoteCertificateReceiveVo> myCertificate(PageQuery pageQuery){
        return certificateReceiveService.queryPageList(new VoteCertificateReceiveBo(),pageQuery);
    }

    /**
     * 提交合作
     */
    @PostMapping("/submit-collaboration")
    public R<Void> submitCollaboration(@Validated(AddGroup.class) @RequestBody VoteInteractBo bo){
        return toAjax(iVoteInteractService.insertByBo(bo));
    }

    /**
     * 提交反馈
     */
    @PostMapping("/submit_feedback")
    public R<Void> submitFeedback(@Validated(AddGroup.class) @RequestBody VoteFeedbackBo bo){
        return toAjax(feedbackService.insertByBo(bo));
    }

    /**
     * 修改我的信息
     * @param wxUser
     * @return
     */
    @PostMapping("/update-user")
    public R<Void> updateUser(@RequestBody WxUser wxUser){
        wxUser.setId(LoginHelper.getUserId());
        boolean b = wxUserService.updateById(wxUser);
        log.info("--- 修改用户信息: {}",b);
        return toAjax(b);
    }

    /**
     * 奖品申领
     * @param bo 领奖信息
     * @return
     */
    @PostMapping("/receive-prize")
    public R<Void> receivePrize(@Validated() @RequestBody ApiPrizeReceiveBo bo){
        log.info("--- 奖品申领用户: {} ,领取数据: {}",LoginHelper.getUserId(),bo);
        VotePrizeReceiveBo prizeReceiveBo = BeanUtil.toBean(bo, VotePrizeReceiveBo.class);

        return toAjax(prizeReceiveService.updateByBo(prizeReceiveBo));
    }

    /**
     *  证书申领
     * @return
     */
    @PostMapping("/receive-certificate")
    public R<Void> receiveCertificate(@RequestBody VoteCertificateReceiveBo bo){
        return toAjax(certificateReceiveService.insertByBo(bo));
    }

}
